﻿using Microsoft.Extensions.Configuration;

namespace MicroCloud.Api.Systems.Areas.Admin.Controllers
{

    public partial class FileController
    {
        /// 获取 文件服务
        protected IFileService FileService => ServiceProvider.GetService<IFileService>();

        /// <summary>
        /// 获取 文件配置选项
        /// </summary>
        protected FileOption FileOption => ServiceProvider.GetService<IConfiguration>()?.GetFileOption();

        /// <summary>
        /// 获取 LOGO Base64字符串
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [NonFunction]
        [AllowAnonymous]
        [DisableRequestSizeLimit]
        [Description("获取 LOGO Base64字符串")]
        public virtual async Task<string> GetLogoBase64String()
        {
            var file = await FileService.GetFileInfoByName(SettingKeyConstants.SiteLogo);
            if (file == null) return null;

            var dicts = new Dictionary<string, string> {
                {"jpg","image/jpeg"},
                {"jpeg","image/jpeg"},
                {"jpe","image/jpeg"},
                {"png","image/png"},
                {"gif","image/gif"},
                {"ico","image/x-ico"},
                {"tif","image/tiff"},
                {"tiff","image/tiff"},
                {"fax","image/fax"},
                {"wbmp","image//vnd.wap.wbmp"},
                {".rp","image/vnd.rn-realpix"}
            };
            var contentTypeStr = "image/png";
            //未知的图片类型
            if (dicts.ContainsKey(file.FileExtension))
            {
                contentTypeStr = dicts[file.FileExtension];
            }

            var fullPath = Path.Combine(FileOption.Root, file.PhysicalPath?.TrimStart(new char[] { '\\' }));
            var bytes = FileHelper.GetFileBytes(fullPath);
            var base64String = bytes.ToBase64String();

            return $"data:{contentTypeStr};base64,{base64String}";
        }

        /// <summary>
        /// LOGO上传
        /// </summary>
        /// <param name="file">请求中的文件信息</param>
        /// <returns></returns>
        [HttpPost]
        [RoleLimit]
        [UnitOfWork]
        [DisableRequestSizeLimit]
        [Description("上传LOGO")]
        [ModuleInfo]
        [ApiExplorerSettings(IgnoreApi = true)]
        public virtual async Task<AjaxResult> UploadLogo([FromForm(Name = "file")] IFormFile file)
        {
            var fileInfo = await FileService.GetFileInfoByName(SettingKeyConstants.SiteLogo);

            using var stream = file.OpenReadStream();
            var uploadFileDto = new UploadInputDto
            {
                FileStream = stream,
                FileName = SettingKeyConstants.SiteLogo,
                FileExtension = Path.GetExtension(file.FileName)//文件扩展名，包含“.”
            };

            OperationResult<FileOutputDto> result = null;
            if (fileInfo == null)
            {
                result = await FileService.UploadAsync(uploadFileDto);
            }
            else
            {
                uploadFileDto.Id = fileInfo.Id;
                result = await FileService.UpdateAsync(uploadFileDto);
            }
            return result.ToAjaxResult();
        }

        /// <summary>
        /// 删除LOGO
        /// </summary>
        [HttpPost]
        [RoleLimit]
        [UnitOfWork]
        [ModuleInfo]
        [Description("删除LOGO")]
        public virtual async Task<AjaxResult> DeleteLogo()
        {
            var fileInfo = await FileService.GetFileInfoByName(SettingKeyConstants.SiteLogo);
            if (fileInfo == null) return AjaxResult.Success();

            await FileService.DeleteAsync(fileInfo.Id);
            return AjaxResult.Success();
        }
    }
}
